'use strict';

const EsEnum = require('../commons/EsEnum');

// 返回码列表
const _RespCodeEnum = new EsEnum([

  {
    name: 'ErrCode_0000',
    code: '0000',
    msg: '操作成功'
  },
  {
    name: 'ErrCode_0001',
    code: '0001',
    msg: '参数错误'
  },
  {
    name: 'ErrCode_0003',
    code: '0003',
    msg: '用户Token失效或不正确。'
  },
  {
    name: 'ErrCode_0004',
    code: '0004',
    msg: '未查到符合条件的数据。'
  },
  {
    name: 'ErrCode_0005',
    code: '0005',
    msg: '用户无此操作权限。'
  },
  {
    name: 'ErrCode_0006',
    code: '0006',
    msg: '要添加的数据重复。'
  },
  {
    name: 'ErrCode_0007',
    code: '0007',
    msg: '此电脑无此操作权限。'
  },
  {
    name: 'ErrCode_0008',
    code: '0008',
    msg: '用户无此操作权限。'
  },
  {
    name: 'ErrCode_1001',
    code: '1001',
    msg: '用户已存在。'
  },
  {
    name: 'ErrCode_1002',
    code: '1002',
    msg: '用户不存在。'
  },
  {
    name: 'ErrCode_1003',
    code: '1003',
    msg: '密码错误'
  },

  {
    name: 'ErrCode_9999',
    code: '9999',
    msg: '失败'
  }

]);

/**
 * 挂在 扩展方法到 response对象上
 */
module.exports = {
  RespCodeEnum: _RespCodeEnum,
  // 成功返回
  success(data) {
    this.status = 200;
    this.body = {
      respCode: this.RespCodeEnum.ErrCode_0000.code,
      respMsg: this.RespCodeEnum.ErrCode_0000.msg,
      respData: data,
    };
    // this.app.logger.debug('成功返回数据：', this.body);
  },
  // 通用失败返回
  fail(msg, data) {
    this.status = 200;
    this.body = {
      respCode: this.RespCodeEnum.ErrCode_9999.code,
      respMsg: msg || this.RespCodeEnum.ErrCode_9999.msg,
      respData: data,
    };
  },
  // 失败返回
  failByCode(code, status, msg, data) {
    const erroCodeName = 'ErrCode_' + code;
    switch (code) {
      case _RespCodeEnum.valueOf(erroCodeName).code:
        this.status = status || 200;
        this.body = {
          respCode: this.RespCodeEnum.valueOf(erroCodeName).code,
          respMsg: msg || this.RespCodeEnum.valueOf(erroCodeName).msg,
          respData: data,
        };
        break;

      default:
        this.status = status || 200;
        this.body = {
          respCode: this.RespCodeEnum.ErrCode_9999.code,
          respMsg: msg || this.RespCodeEnum.ErrCode_9999.msg,
          respData: data,
        };
        break;
    }
  },
  // token错误返回
  tokenError(status) {
    switch (status) {
      case 401:
        this.status = 401;
        this.body = {
          respCode: this.RespCodeEnum.ErrCode_0003.code,
          respMsg: this.RespCodeEnum.ErrCode_0003.msg,
        };
        break;

      case 403:
        this.status = 403;
        this.body = {
          respCode: this.RespCodeEnum.ErrCode_0003.code,
          respMsg: this.RespCodeEnum.ErrCode_0003.msg,
        };
        break;

      default:
        this.status = 401;
        this.body = {
          respCode: this.RespCodeEnum.ErrCode_0003.code,
          respMsg: this.RespCodeEnum.ErrCode_0003.msg,
        };
        break;
    }
  },
};